// v2010.09.21

/*
Copyright (c) 2010 Ben Hendel-Doying

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

package gamesource;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.*;

import javax.swing.JPanel;

import speaker.Speaker;

public abstract class AnimatedPanel extends JPanel implements KeyListener, MouseListener, MouseMotionListener, MouseWheelListener
{
	private static final long serialVersionUID = -8481693521820814579L;

	public abstract void Start();

	// animation
	protected int animation_step = 0;
	private int fps = 0, last_fps = 0;
	private long last_fps_time;

	private int frame_width = 800;
	private int frame_height = 600;

	private boolean playing;

	protected AnimatorThread athread;

	protected GameState state;

	public AnimatedPanel(int width, int height, int fps)
	{
		this(fps);

		frame_width = width;
		frame_height = height;
	}

	public AnimatedPanel(int fps)
	{
		setIgnoreRepaint(true);
		setDoubleBuffered(true);

		athread = new AnimatorThread(this, fps);
		athread.start();

		state = null;
	}

	final public Dimension getPreferredSize()
	{
		return new Dimension(frame_width, frame_height);
	}

	final public void ChangeState(GameState s)
	{
		state = s;
	}

	public void addNotify()
	{
		super.addNotify();
		requestFocus();
	}

	protected void Play()
	{
		addKeyListener(this);
		addMouseListener(this);
		addMouseMotionListener(this);
		addMouseWheelListener(this);

		Speaker.Say(this, "Here!  We!  Go!");

		playing = true;

		while(playing)
		{
			Thread.yield();
		}

		Stop();
	}

	protected void Stop()
	{
		athread.Stop();
	}

	protected void paintComponent(Graphics g)
	{
		super.paintComponent(g);

		if(playing && g != null)
			Render(g);

		fps++;

		if(System.currentTimeMillis() - last_fps_time >= 1000)
		{
			last_fps_time = System.currentTimeMillis();
			last_fps = fps;
			fps = 0;
		}
	}

	final public int FPS() { return last_fps; }
	final public int AnimationStep() { return animation_step; }

	public synchronized void StepAnimation()
	{
		if(playing)
		{
			Animate();
			animation_step = (animation_step + 1) % 3600;
		}
	}

	final public void Quit() { playing = false; }

	final public int Width() { return frame_width; }
	final public int Height() { return frame_height; }
	final public boolean IsPlaying() { return playing; }

	final protected void Render(Graphics g) { if(state != null) state.Render(g); }
	final protected void Animate() { if(state != null) state.Animate(); }

	final public void keyTyped(KeyEvent e) { if(state != null) state.KeyType(e); }
	final public void keyPressed(KeyEvent e) { if(state != null) state.KeyPress(e); }
	final public void keyReleased(KeyEvent e) { if(state != null) state.KeyRelease(e); }
	final public void mouseClicked(MouseEvent e) { if(state != null) state.MouseClick(e); }
	final public void mousePressed(MouseEvent e) { if(state != null) state.MousePress(e); }
	final public void mouseReleased(MouseEvent e) { if(state != null) state.MouseRelease(e); }
	final public void mouseDragged(MouseEvent e) { if(state != null) state.MouseDrag(e); }
	final public void mouseMoved(MouseEvent e) { if(state != null) state.MouseMove(e); }
	final public void mouseWheelMoved(MouseWheelEvent e) { if(state != null) state.MouseWheeled(e); }

	final public void mouseEntered(MouseEvent e) {}
	final public void mouseExited(MouseEvent e) {}
}
